# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
load("//pw_build:compatibility.bzl", "incompatible_with_mcu")
load("//pw_fuzzer:fuzzer.bzl", "pw_cc_fuzz_test")
load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")

package(
    default_visibility = ["//visibility:public"],
    features = [
        "-layering_check",
        "-ctad_warnings",
    ],
)

# Basic target with protocol definitions and no logic, suitable for test
# emulation.
cc_library(
    name = "definitions",
    hdrs = [
        "public/pw_bluetooth_sapphire/internal/host/l2cap/frame_headers.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/l2cap_defs.h",
    ],
    features = ["-conversion_warnings"],
    strip_include_prefix = "public",
    tags = ["noclangtidy"],
    deps = [
        "//pw_bluetooth_sapphire/host/common",
        "//pw_bytes",
        "//pw_chrono:system_clock",
    ],
)

cc_library(
    name = "l2cap",
    srcs = [
        "a2dp_offload_manager.cc",
        "basic_mode_rx_engine.cc",
        "basic_mode_tx_engine.cc",
        "bredr_command_handler.cc",
        "bredr_dynamic_channel.cc",
        "bredr_signaling_channel.cc",
        "channel.cc",
        "channel_configuration.cc",
        "channel_manager.cc",
        "command_handler.cc",
        "credit_based_flow_control_rx_engine.cc",
        "credit_based_flow_control_tx_engine.cc",
        "dynamic_channel.cc",
        "dynamic_channel_registry.cc",
        "enhanced_retransmission_mode_engines.cc",
        "enhanced_retransmission_mode_rx_engine.cc",
        "enhanced_retransmission_mode_tx_engine.cc",
        "fcs.cc",
        "fragmenter.cc",
        "le_dynamic_channel.cc",
        "le_signaling_channel.cc",
        "logical_link.cc",
        "low_energy_command_handler.cc",
        "pdu.cc",
        "recombiner.cc",
        "scoped_channel.cc",
        "signaling_channel.cc",
        "types.cc",
    ],
    hdrs = [
        "public/pw_bluetooth_sapphire/internal/host/l2cap/a2dp_offload_manager.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/basic_mode_rx_engine.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/basic_mode_tx_engine.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/bredr_command_handler.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/bredr_dynamic_channel.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/bredr_signaling_channel.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/channel.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/channel_configuration.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/channel_manager.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/command_handler.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/credit_based_flow_control_rx_engine.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/credit_based_flow_control_tx_engine.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/dynamic_channel.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/dynamic_channel_registry.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/enhanced_retransmission_mode_engines.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/enhanced_retransmission_mode_rx_engine.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/enhanced_retransmission_mode_tx_engine.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/fcs.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/fragmenter.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/le_dynamic_channel.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/le_signaling_channel.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/logical_link.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/low_energy_command_handler.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/pdu.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/recombiner.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/rx_engine.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/scoped_channel.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/signaling_channel.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/tx_engine.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/types.h",
    ],
    features = ["-conversion_warnings"],
    implementation_deps = [
        "//pw_bluetooth:emboss_l2cap_frames",
        "//pw_preprocessor",
    ],
    strip_include_prefix = "public",
    tags = ["noclangtidy"],
    target_compatible_with = select({
        "@platforms//os:fuchsia": [],
        "@platforms//os:linux": [],
        "@platforms//os:macos": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        ":definitions",
        "//pw_bluetooth",
        "//pw_bluetooth:emboss_hci_android",
        "//pw_bluetooth_sapphire:lease",
        "//pw_bluetooth_sapphire/host/common",
        "//pw_bluetooth_sapphire/host/hci",
        "//pw_bluetooth_sapphire/host/sm:definitions",
        "//pw_bluetooth_sapphire/host/transport",
        "//pw_string",
        "//third_party/fuchsia:fit",
    ],
)

# Separate from :testing to avoid a dependency cycle.
cc_library(
    name = "channel_manager_mock_controller_test_fixture",
    testonly = True,
    hdrs = [
        "public/pw_bluetooth_sapphire/internal/host/l2cap/channel_manager_mock_controller_test_fixture.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":l2cap",
        ":testing",
        "//pw_bluetooth_sapphire/host/testing",
        "//pw_bluetooth_sapphire/host/testing:mock_controller",
    ],
)

cc_library(
    name = "testing",
    testonly = True,
    srcs = [
        "fake_channel.cc",
        "fake_channel_test.cc",
        "fake_l2cap.cc",
        "fake_signaling_channel.cc",
        "mock_channel_test.cc",
        "test_packets.cc",
    ],
    hdrs = [
        "public/pw_bluetooth_sapphire/internal/host/l2cap/fake_channel.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/fake_channel_test.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/fake_l2cap.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/fake_signaling_channel.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/fake_tx_channel.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/mock_channel_test.h",
        "public/pw_bluetooth_sapphire/internal/host/l2cap/test_packets.h",
    ],
    features = ["-conversion_warnings"],
    implementation_deps = [
        "//pw_bluetooth_sapphire/host/testing:gtest_helpers",
        "//pw_bluetooth_sapphire/host/testing:test_helpers",
    ],
    strip_include_prefix = "public",
    deps = [
        ":l2cap",
        "//pw_async:fake_dispatcher_fixture",
        "//pw_async:heap_dispatcher",
        "//pw_bluetooth_sapphire/host/common",
        "//pw_bluetooth_sapphire/host/hci-spec",
        "//third_party/fuchsia:fit",
    ],
)

pw_cc_test(
    name = "l2cap_test",
    srcs = [
        "a2dp_offload_manager_test.cc",
        "basic_mode_rx_engine_test.cc",
        "basic_mode_tx_engine_test.cc",
        "bredr_command_handler_test.cc",
        "bredr_dynamic_channel_test.cc",
        "bredr_signaling_channel_test.cc",
        "channel_configuration_test.cc",
        "channel_manager_test.cc",
        "channel_test.cc",
        "command_handler_test.cc",
        "credit_based_flow_control_rx_engine_test.cc",
        "credit_based_flow_control_tx_engine_test.cc",
        "dynamic_channel_registry_test.cc",
        "enhanced_retransmission_mode_engines_test.cc",
        "enhanced_retransmission_mode_rx_engine_test.cc",
        "enhanced_retransmission_mode_tx_engine_test.cc",
        "fcs_test.cc",
        "fragmenter_test.cc",
        "frame_headers_test.cc",
        "le_dynamic_channel_test.cc",
        "le_signaling_channel_test.cc",
        "logical_link_test.cc",
        "low_energy_command_handler_test.cc",
        "pdu_test.cc",
        "recombiner_test.cc",
        "scoped_channel_test.cc",
        "signaling_channel_test.cc",
        "types_test.cc",
    ],
    features = ["-conversion_warnings"],
    test_main = "//pw_bluetooth_sapphire/host/testing:gtest_main",
    deps = [
        ":channel_manager_mock_controller_test_fixture",
        ":l2cap",
        ":testing",
        "//pw_async:fake_dispatcher_fixture",
        "//pw_bluetooth_sapphire:null_lease_provider",
        "//pw_bluetooth_sapphire/host/hci",
        "//pw_bluetooth_sapphire/host/hci:testing",
        "//pw_bluetooth_sapphire/host/testing",
        "//pw_bluetooth_sapphire/host/testing:gtest_helpers",
        "//pw_bluetooth_sapphire/host/testing:test_helpers",
        "//pw_bluetooth_sapphire/host/transport:testing",
    ],
)

pw_cc_fuzz_test(
    name = "basic_mode_rx_engine_fuzzer",
    srcs = ["basic_mode_rx_engine_fuzztest.cc"],
    deps = [
        ":l2cap",
        ":testing",
    ],
)

pw_cc_fuzz_test(
    name = "enhanced_retransmission_mode_engines_fuzzer",
    srcs = ["enhanced_retransmission_mode_engines_fuzztest.cc"],
    deps = [
        ":l2cap",
        ":testing",
    ],
)

pw_cc_fuzz_test(
    name = "bredr_dynamic_channel_registry_fuzzer",
    srcs = ["bredr_dynamic_channel_registry_fuzztest.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":l2cap",
        ":testing",
        "//pw_bluetooth_sapphire:null_lease_provider",
        "//pw_random:fuzzer_generator",
    ],
)

pw_cc_fuzz_test(
    name = "channel_configuration_fuzzer",
    srcs = ["channel_configuration_fuzztest.cc"],
    deps = [
        ":l2cap",
        ":testing",
    ],
)

pw_cc_fuzz_test(
    name = "common_handler_fuzzer",
    srcs = ["common_handler_fuzztest.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":l2cap",
        ":testing",
    ],
)

pw_cc_fuzz_test(
    name = "l2cap_fuzzer",
    srcs = ["l2cap_fuzztest.cc"],
    features = ["-conversion_warnings"],
    target_compatible_with = select({
        "//pw_unit_test:backend_is_googletest": [],
        "@platforms//os:fuchsia": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        ":l2cap",
        "//pw_bluetooth_sapphire/host/testing",
        "//pw_bluetooth_sapphire/host/testing:controller_test_double_base",
        "//pw_random:fuzzer_generator",
    ],
)

sphinx_docs_library(
    name = "docs",
    srcs = [
        "docs.rst",
    ],
    target_compatible_with = incompatible_with_mcu(),
)
